<?php

namespace app\admin\controller;

use app\admin\model\Banner as BannerModel;
use app\BaseController;

class Banner extends BaseController
{
    /* 获取 */
    public function get()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $cid = input('cid', 0, 'intval');
        $type = input('type', '');
        $data = BannerModel::where(['cid' => $cid])->value($type);
        if ($data) {;
            return show(config('status.success'), '查询成功', unserialize($data));
        } else {
            return show(config('status.error_find'), '查询失败');
        }
    }

    /* 修改 */
    public function modify()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $cid = input('cid', 0, 'intval');
        $type = input('type', '');
        $image1 = input('image1', '');
        $image2 = input('image2', '');
        $image3 = input('image3', '');
        $arr = [$image1, $image2, $image3];
        $str = serialize($arr);

        $one = BannerModel::where('cid', $cid)->find();
        if (!$one) {
            switch ($type) {
                case 'home':
                    $result = BannerModel::create([
                        'cid' => $cid,
                        'home' => $str
                    ]);
                    break;
                case 'guide':
                    $result = BannerModel::create([
                        'cid' => $cid,
                        'guide' => $str
                    ]);
                    break;
                case 'party':
                    $result = BannerModel::create([
                        'cid' => $cid,
                        'party' => $str
                    ]);
                    break;
            }
        } else {
            switch ($type) {
                case 'home':
                    $result = BannerModel::update([
                        'home' => $str
                    ], ['cid' => $cid]);
                    break;
                case 'guide':
                    $result = BannerModel::update([
                        'guide' => $str
                    ], ['cid' => $cid]);
                    break;
                case 'party':
                    $result = BannerModel::update([
                        'party' => $str
                    ], ['cid' => $cid]);
                    break;
            }
        }

        if ($result) {
            return show(config('status.success'), '修改成功');
        } else {
            return show(config('status.error_update'), '修改失败');
        }
    }
}
